package ytdp.careagle.com.ytdp.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wx.wheelview.adapter.MyWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.activity.BaseActivity;

public class SelectTimeDialog extends BaseActivity {

    @Bind(R.id.tv_cancel)
    TextView tvCancel;
    @Bind(R.id.tv_ok)
    TextView tvOk;
    @Bind(R.id.main_wheelview)
    WheelView mainWheelview;
    @Bind(R.id.sub_wheelview)
    WheelView subWheelview;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.ll_viewGroup)
    LinearLayout llViewGroup;
    @Bind(R.id.ll_gen)
    RelativeLayout llGen;
    private int year;
    private int month;
    private ArrayList<String> years;
    private ArrayList<String> months;

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_time_dialog;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_ok, R.id.ll_gen})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_gen:
                finish();
                break;
            case R.id.tv_ok:
                selectOk();
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    private void selectOk() {
        Intent intent = new Intent();
        Object mainWheelviewSelectedItem = mainWheelview.getSelectionItem();
        Object subWheelviewSelectedItem = subWheelview.getSelectionItem();
        intent.putExtra("yearSelected", mainWheelviewSelectedItem.toString());
        intent.putExtra("monthSelected", subWheelviewSelectedItem.toString());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_down);
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        int lastYear = intent.getIntExtra("year", -1);
        int lastMonth = intent.getIntExtra("month", -1);
        int bottom = intent.getIntExtra("bottom", -1);
        bottom += getResources().getDimension(R.dimen.dp15);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) llViewGroup.getLayoutParams();
        layoutParams.setMargins(0, bottom, 0, 0);
        initDatas();
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        mainWheelview.setSkin(WheelView.Skin.Holo);
        mainWheelview.setWheelSize(5);
        subWheelview.setWheelSize(5);
        mainWheelview.setWheelAdapter(new MyWheelAdapter(this));
        subWheelview.setWheelAdapter(new MyWheelAdapter(this));
        subWheelview.setSkin(WheelView.Skin.Holo);
        mainWheelview.setStyle(style);
        style.selectedTextSize = 30;
        style.textSize = 24;
        subWheelview.setStyle(style);
        mainWheelview.setWheelData(years);
        subWheelview.setWheelData(months);
        if (lastYear == -1) {
            mainWheelview.setSelection(years.size());
        } else {
            mainWheelview.setSelection(lastYear - 1970);
        }
        if (lastMonth == -1) {
            subWheelview.setSelection(this.month - 1);
        } else {
            subWheelview.setSelection(lastMonth - 1);
        }
    }

    private void initDatas() {
        Calendar now = Calendar.getInstance();
        year = now.get(Calendar.YEAR);
        month = now.get(Calendar.MONTH) + 1;
        years = new ArrayList<>();
        for (int i = 1970; i <= year; i++) {
            years.add(i + "");
        }
        months = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            months.add(i + "");
        }
    }
}
